package sunwin.yoms.web.controller.resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.comm.YomsHelper;
import sunwin.yoms.domain.resource.SubscribeReportFormsDomain;
import sunwin.yoms.service.resource.SubscribeOrderService;
import sunwin.yoms.service.rmiServer.ISubscribeOrder;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsAjaxAnswer;
import sunwin.yoms.web.comm.YomsController;

import java.util.Collection;

/**
 * Created by watson on 2015/11/17.
 */
@Controller
public class StadiumStatisticController {

    @Autowired
    private SubscribeOrderService subscribeOrderService;

    private final String StadiumStatistic = "resource/stadium.statistic";
    private final String StadiumStatisticDetail = "resource/stadium.statistic-detailed";

    @RequestMapping(value = R.StadiumStatistic)
    @YomsController(functionNo = YomsFunction.StadiumStatistic, viewName = StadiumStatistic)
    public void stadiumStatistis() throws YomsException {
    }

    @RequestMapping(value = R.StadiumStatisticDetail)
    @YomsController(functionNo = YomsFunction.StadiumStatisticDetail, viewName = StadiumStatisticDetail)
    public void stadiumStatistisDeatail() throws YomsException {
    }

    /**
     * 根据时间类型获取场馆概要统计
     * @param dateType 1=7天;2=30天
     * @return
     */
    @RequestMapping(value = R.GetStadiumStatisticByDateType, method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @YomsController(functionNo = YomsFunction.StadiumStatistic)
    @ResponseBody
    public Object getStadiumGroupStatisticByDateType(int dateType) {

        Collection<SubscribeReportFormsDomain> subscribeReportFormsDomains = subscribeOrderService.getStadiumGroupStatisticByDateType(dateType);

        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(subscribeReportFormsDomains);

        YomsAjaxAnswer answer = new YomsAjaxAnswer();
        answer.setSuccess(true);
        answer.setData(jsonArray.toString());
        System.out.println(jsonArray.toString());

        JSONObject jsonObject = JSONObject.fromObject(answer);
        String jsonResult = jsonObject.toString();
        System.out.println(jsonResult);

        return jsonResult;
    }

    /**
     * 根据场馆分组ID获取该分组下各个场馆的统计
     * @param dateType 1=7天;2=30天
     * @param gid 场馆分组ID
     * @return
     */
    @RequestMapping(value = R.GetStadiumStatisticByDateTypeWithGid, method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @YomsController(functionNo = YomsFunction.StadiumStatistic)
    @ResponseBody
    public String getStadiumGroupStatisticByDateTypeWithGid(int dateType, int gid) {

        Collection<SubscribeReportFormsDomain> subscribeReportFormsDomains = subscribeOrderService.getStadiumGroupStatisticByDateTypeWithGid(dateType, gid);

        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(subscribeReportFormsDomains);

        YomsAjaxAnswer answer = new YomsAjaxAnswer();
        answer.setSuccess(true);
        answer.setData(jsonArray.toString());

        JSONObject jsonObject = JSONObject.fromObject(answer);

        return jsonObject.toString();
    }
}
